package com.onlineauction.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.onlineauction.entities.Auction;
import com.onlineauction.entities.Item;
import com.onlineauction.helpers.Constants;
import com.onlineauction.helpers.Constants.Category;

public class ViewAuctionServlet extends HttpServlet {


	private static final long serialVersionUID = 2760553635085983719L;

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,
			ServletException {

		HttpSession session = request.getSession(true);
		String username = (String) session.getAttribute(Constants.USERNAME);

		// user not logged in
		if (username == null) {
			response.sendRedirect("/LoginAccount");
			return;
		}

		long auctionID = -1;
		try {
			 auctionID = Long.parseLong(request.getParameter(Constants.AUCTION_ID));
		} catch (Exception e) {
		}

		if (auctionID == -1){
			String message = "Auction doesn't exist";
			request.setAttribute(Constants.MESSAGE, message);
			request.getRequestDispatcher("/manageAuction.jsp").forward(request, response);
			return;
		}
		
		Auction auction = new Auction(auctionID);
		if(auction != null)
		{
			request.setAttribute(Constants.AUCTION, auction);		
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("/viewAuction.jsp");
			requestDispatcher.forward(request, response);
		}
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		doGet(request, response);
	}
}
